﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 网站应用程序        ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System;
using System.Threading;
using System.Web.Mvc;
using Wedn.Net.Common;
using Wedn.Net.Controller;
using Wedn.Net.Route;
using Wedn.Net.TEngine;
using Wedn.Net.UI.Config;
using Wedn.Net.Utility;

namespace Wedn.Net.UI
{
    /// <summary>
    /// 网站应用程序
    /// </summary>
    public static class WednApplication
    {
        #region 初始化操作 +static void Init()
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            System.Web.Routing.RouteTable.Routes.Clear();
            //加载全局设置
            Setting.Init(BLLFactory.BLLFactory.Instance.OptionManager.GetSetting());
            //注册区域信息
            AreaRegistration.RegisterAllAreas();
            //注册WebAPI
            //WebApiConfig.Register(GlobalConfiguration.Configuration);
            //注册控制器配置
            ControllerConfig.RegisterDefaultNamespace();
            //注册过滤器
            FilterConfig.RegisterGlobalFilters();
            //注册路由
            RouteConfig.RegisterRoutes();
            //注册捆绑资源
            BundleConfig.RegisterBundles();
            //注册模版引擎
            TemplateConfig.RegisterViewEngines();
            //开始统计
            var thread = new Thread(() =>
            {
                while (true)
                {
                    Thread.Sleep(Common.Config.StatisticsTimespan.ToInt(300000));
                    //更新流量次数
                    BLLFactory.BLLFactory.Instance.OptionManager.UpdateValue("site_view_count", Setting.SiteViewCount.ToString());
                    BLLFactory.BLLFactory.Instance.OptionManager.SaveChanges();
                }
            });
            thread.Start();
        } 
        #endregion
    }

    /// <summary>
    /// 网站应用程序全局类型
    /// </summary>
    public class Global : System.Web.HttpApplication
    {
        /// <summary>
        /// 网站程序开始事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_Start(object sender, EventArgs e)
        {
            WednApplication.Init();
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        }

        /// <summary>
        /// 会话开始事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Session_Start(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 请求开始时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_BeginRequest(object sender, EventArgs e)
        {

            //WednApplication.Init();
        }

        /// <summary>
        /// 验证请求
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 程序错误事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_Error(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 会话结束事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Session_End(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 程序结束事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}
